//
//  SourceViewController.m
//  Globalstone
//
//  Created by 辰领科技 on 16/7/18.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "SourceViewController.h"
#import "StoneStreetViewController.h"
#import "SearchSupplyResultViewController.h"

@interface SourceViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroview;
@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet UIButton *searchButton;

@end

@implementation SourceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initViewLayout];
}
- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroview];
    _scroview.contentSize = flexibleSize(CGSizeMake(0, 505), NO);
    self.view.backgroundColor = [UIColor colorWithWhite:0.953 alpha:1.000];
    
    _textField.delegate = self;
    _textField.returnKeyType = UIReturnKeySearch;
}

- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark - 搜索
- (IBAction)search:(id)sender {
    [self hideKeyBoard];
    [self searchStone];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self hideKeyBoard];
    [self searchStone];
    return YES;
}

#pragma mark - 确认
- (IBAction)confirm:(id)sender {
    [self hideKeyBoard];
    [self searchStone];
    
}

- (void)searchStone{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    self.shareApi.delegate=self;
    [dic setObject:_textField.text forKey:@"orderSysId"];
    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
    [self.shareApi  requestOperationWithDic:dic withPath:GS_querSourcingsourcingDetail withHttpType:@"POST" withTag:GS_GS_querSourcingsourcingDetail];
}


- (void)showAlertView{
    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"对不起，您无权访问石材一条街版块" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag == GS_GS_querSourcingsourcingDetail){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            NSDictionary * dic= database[@"result"];
            if (![Common getLoginStatus]) {
                [[AppDelegate sharedAppDelegate]ShowLoginPage];
                return;
            }
            NSInteger userType = [[Common getLoginInfo][@"userType"]integerValue];
            if ([dic[@"orderBook"]isEqual:[NSNull null]]&&[dic[@"orderContainer"]isEqual:[NSNull null]]&&[dic[@"orderStone"]isEqual:[NSNull null]]) {
            if (userType == 9||userType==10||userType==11) {
                [self showAlertView];
            }else{
                UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
                StoneStreetViewController * page = [storyBoard instantiateViewControllerWithIdentifier:@"StoneStreet"];
                page.searchStr = _textField.text;
                [[Common getNavigation]pushViewController:page];
            }
            }else{
                if (userType ==4 ||userType ==5 ||userType ==6 ||userType ==12 ) {
               
                    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"对不起，您无权访问供应链版块" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alertView show];
                }else{
                    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
                    SearchSupplyResultViewController * page = [storyBoard instantiateViewControllerWithIdentifier:@"SearchSupplyResult"];
                    page.infoDic = dic;
                    [[Common getNavigation]pushViewController:page];
                }
               
           }
            
        }
            
       
    }
}


-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    }


@end
